<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>付款</title>
		<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, user-scalable=no">
		<link rel="stylesheet" type="text/css" href="css/payment2.css?id=11" />
		<script src="js/jq.js"></script>
		<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
	</head>

	<body>
		<h2 style="color: #D92735;margin: 1rem;">请支付尾款</h2>
		<div id="c" style="width: 100%;height: 100%;display: flex;flex-wrap: wrap;">
			<div class="onc" style="background-color: #D92735;font-size: 2rem;height: 3rem;border: #D92735 solid 1px;margin: 2rem;color: #FFFFFF;text-align: center;padding: 0;">
				<span id="pr"></span>
				<span id="prs"></span>元
			</div>
		</div>
		<input type="hidden" id="price" value="" />
		<input type="hidden" id='type' value="" />
		<div class="tishi">
			订单完成后，可在个人中心”我的卡包“ <br>查看所赠充电卡券。
		</div>
		<div class="savebox">
			<div class="save">支付尾款</div>
			<div class="save2">支付宝6期免息</div>
			<div class="save1" style="display: none; line-height: 50px;text-align: center;color: #FFFFFF;background: #D92735;font-size: 25px;font-weight: 500;border-radius: 999px;">申请退订金</div>
		</div>
	</body>
	<script type="text/javascript">
		function getUrldata(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if (r != null) return decodeURI(r[2]);
			return null;
		}
		//为上一个页面传过来的参名
		var Bid = getUrldata('bid');
		var stringArr = Bid.split(':');
		//判断终端
		var u = navigator.userAgent;
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端

		//付尾款
		$('.save').click(function() {
			var price = $('#prs').text();
			var pr = price * 100;
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/user_order",
				data: {
					userid_locked: stringArr[1],
					price:pr
				},
				dataType: "json",
				async: true,
				success: function(res) {
					if(res.result_code == 201){
						if (isWeiXin()) {
							(function() {
								window.alert = function(name) {
									var iframe = document.createElement("IFRAME");
									iframe.style.display = "none";
									iframe.setAttribute("src", 'data:text/plain');
									document.documentElement.appendChild(iframe);
									window.frames[0].window.alert(name);
									iframe.parentNode.removeChild(iframe);
								}
							})();
							alert('您支付的金额有变动，如有疑问请联系运维人员。');
							location.reload();
							return
						}
						if (isiOS) {
							//ios
							window.webkit.messageHandlers.showAlert.postMessage('您支付的金额有变动，如有疑问请联系运维人员。');
							location.reload();
							return
						}
						if (isAndroid) {
							//Android
							window.TiantianCharge_Android.showAlert('您支付的金额有变动，如有疑问请联系运维人员。');
							location.reload();
							return
						}
					}
					if (res.result_code == 200) {
						var data = res.data;
						if (data == null) {
							//跳回首页 
							window.location.href = 'index.html?Uid='+ stringArr[1];
						} else {
							if (data.course == 2) {
								//微信支付
								if (isWeiXin()) {
									// 判断小程序的环境
									if (window.__wxjs_environment === 'miniprogram') {
										wx.miniProgram.reLaunch({
											url: `/pages/pay/pay?fee=${pr} + &orderId=${stringArr[0]}`
										})
									}
								}
								//判断机型
								if (isiOS) {
									//ios 支付
									var pay = pr + '!' + stringArr[0]; //支付参数拼接，请勿修改格式
									window.webkit.messageHandlers.pay.postMessage(pay);
								}
								if (isAndroid) {
									//Android 支付
									window.TiantianCharge_Android.pay(pr,stringArr[0]);
								}
							}
						}
					}
				}
			});
		});
		//分期
		$('.save2').click(function() {
			var price = $('#prs').text();
			var pr = price * 100;
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/user_order",
				data: {
					userid_locked: stringArr[1]
				},
				dataType: "json",
				async: true,
				success: function(res) {
					
					if(res.result_code == 201){
						if (isWeiXin()) {
							(function() {
								window.alert = function(name) {
									var iframe = document.createElement("IFRAME");
									iframe.style.display = "none";
									iframe.setAttribute("src", 'data:text/plain');
									document.documentElement.appendChild(iframe);
									window.frames[0].window.alert(name);
									iframe.parentNode.removeChild(iframe);
								}
							})();
							alert('您支付的金额有变动，如有疑问请联系运维人员。');
							location.reload();
							return
						}
						if (isiOS) {
							//ios
							window.webkit.messageHandlers.showAlert.postMessage('您支付的金额有变动，如有疑问请联系运维人员。');
							location.reload();
							return
						}
						if (isAndroid) {
							//Android
							window.TiantianCharge_Android.showAlert('您支付的金额有变动，如有疑问请联系运维人员。');
							location.reload();
							return
						}
					}
					
					if (res.result_code == 200) {
						var data = res.data;
						if (data == null) {
							//跳回首页 
							window.location.href = 'index.html';
						} else {
							if (data.course == 2) {
								//微信支付
								if (isWeiXin()) {
									// 判断小程序的环境
								(function() {
									window.alert = function(name) {
										var iframe = document.createElement("IFRAME");
										iframe.style.display = "none";
										iframe.setAttribute("src", 'data:text/plain');
										document.documentElement.appendChild(iframe);
										window.frames[0].window.alert(name);
										iframe.parentNode.removeChild(iframe);
									}
								})();
								alert("请下载'天天充电APP'完成分期付款操作");
								return
								}
								//APP分期
								var value3 = (pr / 3)/100;
								var value6 = (pr / 6)/100;
								var fen3 = '支付宝分3期每期：'; 
								var fen6 = '支付宝分6期每期：'; 
								if (isiOS) {
									//ios 支付
									window.webkit.messageHandlers.alipay.postMessage({
										price: pr,
										order_id: stringArr[0],
										value3:fen3+value3.toFixed(2)+'元',
										value6:fen6+value6.toFixed(2)+'元',
									});
								}
								if (isAndroid) {
									var val3  = fen3+value3.toFixed(2)+'元';
									var val6  = fen6+value6.toFixed(2)+'元';
									window.TiantianCharge_Android.PaymentByInstalments(pr,stringArr[0],val3,val6);
								}
							}
						}
					}
				}
			});
		});

		//退订金
		$('.save1').click(function() {
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/user_order_back",
				data: {
					userid_locked: stringArr[1],
					battery_id: stringArr[0]
				},
				dataType: "json",
				async: true,
				success: function(res) {
					if (isWeiXin()) {
						(function() {
							window.alert = function(name) {
								var iframe = document.createElement("IFRAME");
								iframe.style.display = "none";
								iframe.setAttribute("src", 'data:text/plain');
								document.documentElement.appendChild(iframe);
								window.frames[0].window.alert(name);
								iframe.parentNode.removeChild(iframe);
							}
						})();
						alert('退款申请已提交，请您耐心等候');
						window.history.back(-1);
						return
					}
					if (isiOS) {
						//ios
						window.webkit.messageHandlers.showAlert.postMessage('退款申请已提交，请您耐心等候');
						window.history.back(-1);
						return
					}
					if (isAndroid) {
						//Android
						window.TiantianCharge_Android.showAlert('退款申请已提交，请您耐心等候');
						window.history.back(-1);
						return
					}
				}
			});
		})
		//判断微信
		function isWeiXin() {

			var ua = window.navigator.userAgent.toLowerCase();
			if (ua.match(/MicroMessenger/i) == 'micromessenger') {
				return true; // 是微信端
			} else {
				return false;
			}
		}

		$(function() {
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/maint/getBattery",
				data: {
					id: stringArr[0]
				},
				dataType: "json",
				async: true,
				success: function(res) {
					if (res.code == 200) {
						document.getElementById("pr").innerHTML = res.data.size;
						document.getElementById("prs").innerHTML = res.data.price;
					}
				}
			});
		})
		//判断订单状态
		setTimeout(function() {
			$.ajax({
				type: "POST",
				url: "https://api.cd1a.cn/index.php/activity/swap4/user_order",
				data: {
					userid_locked: stringArr[1]
				},
				dataType: "json",
				async: true,
				success: function(res) {
					// console.log(res);
					if (res.result_code == 200) {
						var data = res.data;
						if (data == null) {
							return
						}
						// 1，已旧换新预定；2,支付定金；3，分配运维，4，运维联系用户；5，预约上门检测，6，确认商品id；7，支付尾款；8，预约换电时间；9上门换点完成
						if (data.course < 3) {
							$("#type").attr("value", 2);
							$(".save1").show();
						}
					}

				}
			});
		}, 10);
	</script>

</html>
